package com.example.service.impl;

import com.example.dao.ProductAllotApplyDao;
import com.example.dao.ProductInventoryDao;
import com.example.pojo.dto.AllotProcessingDTO;
import com.example.pojo.dto.PageDTO;
import com.example.pojo.dto.SaveProductAllotApplyDTO;
import com.example.pojo.dto.UpdateProductAllotApplyDTO;
import com.example.pojo.orm.ProductAllotApply;
import com.example.pojo.orm.ProductInventory;
import com.example.pojo.vo.PageVO;
import com.example.service.ifac.ProductAllotApplyService;
import com.example.util.JsonResult;
import com.example.util.ResultCodeEM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 季鑫利
 * @description 商品调拨申请接口实现
 */
@Service
public class ProductAllotApplyServiceImpl implements ProductAllotApplyService {

    /**
     * 商品调拨申请dao
     */
    @Autowired
    private ProductAllotApplyDao productAllotApplyDao;

    /**
     * 商品库存dao
     */
    @Autowired
    private ProductInventoryDao productInventoryDao;

    /**
     * 新增调拨申请单
     *
     * @param saveProductAllotApplyDTO 新增调拨申请单信息
     * @return 操作结果
     * @author 季鑫利
     */
    @Override
    public JsonResult saveProductAllotApply(@RequestBody SaveProductAllotApplyDTO saveProductAllotApplyDTO) {
        JsonResult result = new JsonResult();
        //对参数的合理性判断
        if (saveProductAllotApplyDTO.getAllotNumber() == null
                || saveProductAllotApplyDTO.getAllotNumber() <= 0
                || saveProductAllotApplyDTO.getApplicant().isEmpty()
                || saveProductAllotApplyDTO.getCounterNumber().isEmpty()
                || saveProductAllotApplyDTO.getModelNumber().isEmpty()
                || saveProductAllotApplyDTO.getTypeId() == null
                || saveProductAllotApplyDTO.getTypeId() <= 0
                || saveProductAllotApplyDTO.getProductId() == null
                || saveProductAllotApplyDTO.getProductId() <= 0
                || saveProductAllotApplyDTO.getTypeName().isEmpty()
                || saveProductAllotApplyDTO.getUnit() == null
                || saveProductAllotApplyDTO.getUnit() <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数不可为空");
            return result;
        }

        boolean bRet = productAllotApplyDao.saveProductAllotApply(saveProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("新增调拨单失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("新增调拨单成功");
        return result;
    }

    /**
     * 根据调拨申请单id删除申请单
     *
     * @param id 调拨申请单id
     * @return 操作结果
     * @author 季鑫利
     */
    @Override
    public JsonResult removeProductAllotApply(@RequestParam Integer id) {
        JsonResult result = new JsonResult();
        //对参数的合理性判断
        if (id == null || id <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("异常id");
            return result;
        }
        boolean bRet = productAllotApplyDao.removeProductAllotApply(id) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("删除调拨单失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("删除调拨单成功");
        return result;
    }

    /**
     * 更新调拨单
     *
     * @param updateProductAllotApplyDTO 更新调拨申请参数
     * @return 操作结果
     * @author 季鑫利
     */
    @Override
    public JsonResult updateProductAllotApply(@RequestBody UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        //首先根据商品id查询调拨申请单
        //检验参数
        JsonResult result = checkUpdateProductAllotApplyDTO(updateProductAllotApplyDTO);
        boolean bRet = productAllotApplyDao.updateProductAllotApply(updateProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新调拨单失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("更新调拨单成功");
        return result;
    }

    /**
     * 查询所有调拨申请单
     *
     * @param pageDTO 页面参数
     * @return 所有调拨申请单
     * @author 季鑫利
     */
    @Override
    public JsonResult<List<ProductAllotApply>> queryProductAllotApply(@RequestBody PageDTO pageDTO) {
        JsonResult result = new JsonResult();
        if (pageDTO.getPageNum() == null
                || pageDTO.getPageNum() <= 0
                || pageDTO.getTake() == null
                || pageDTO.getTake() <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数有误");
            return result;
        }
        //查询总条数
        int count = productAllotApplyDao.count();
        PageVO<ProductAllotApply> pageVO = new PageVO<>();
        //存在就返回数据，不存在返回空数组
        List<ProductAllotApply> productAllotApplies = count > 0 ? productAllotApplyDao.queryProductAllotApply(pageDTO) : new ArrayList<>();
        pageVO.setCount(count);
        pageVO.setList(productAllotApplies);
        result.setStatus(ResultCodeEM.OK);
        result.setData(pageVO);
        return result;
    }

    /**
     * 根据商品类别id查询
     *
     * @param typeId 商品类别id
     * @return 调拨申请单信息
     * @author 季鑫利
     */
    @Override
    public JsonResult<ProductAllotApply> queryProductAllotApplyByTypeId(@RequestParam Integer typeId) {
        JsonResult result = new JsonResult();
        //对参数的合理性判断
        if (typeId == null || typeId <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("商品类别id异常");
            return result;
        }
        //初始化调拨申请
        List<ProductAllotApply> productAllotApplies;
        try {
            productAllotApplies = productAllotApplyDao.queryProductAllotApplyByTypeId(typeId);
        } catch (Exception e) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setData(e.getMessage());
            return result;
        }
        if (productAllotApplies.size() <= 0) {
            result.setStatus(ResultCodeEM.NOCONTENT);
            result.setMessage("该商品类别调拨单不存在");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("查询成功");
        result.setData(productAllotApplies);
        return result;
    }

    /**
     * 调拨处理--发货
     *
     * @param updateProductAllotApplyDTO 调拨处理参数实体
     * @author 季鑫利
     */
    @Override
    public JsonResult shipmentsAllocateProcessing(@RequestBody UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        //检验参数
        JsonResult result = checkUpdateProductAllotApplyDTO(updateProductAllotApplyDTO);
        //定义布尔值
        boolean bRet;
        //先查询该商品类别有无库存
        ProductInventory productInventory = productInventoryDao.queryProductInventoryById(updateProductAllotApplyDTO.getProductId());
        if (productInventory == null) {
            //断货修改商品状态
            //先查出调拨申请数据
            ProductAllotApply productAllotApply = productAllotApplyDao.queryProductAllotApplyById(updateProductAllotApplyDTO.getId());
            updateProductAllotApplyDTO.setApplyStatus(2);
            bRet = productAllotApplyDao.updateProductAllotApply(updateProductAllotApplyDTO) > 0;
            if (!bRet) {
                result.setStatus(ResultCodeEM.ERROR);
                result.setMessage("更新断货信息失败！");
                return result;
            }
            result.setStatus(ResultCodeEM.OK);
            result.setMessage("商品断货：更新断货信息成功");
            return result;
        }
        //有库存
        //发货处理，库存量减去调拨数量
        productInventory.setSurplus(productInventory.getSurplus() - updateProductAllotApplyDTO.getAllotNumber());
        bRet = productInventoryDao.updateProductInventory(updateProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新库存失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("发货成功");
        return result;
    }

    /**
     * 调拨处理--退货
     *
     * @param updateProductAllotApplyDTO 更新库存处理参数实体
     * @author 季鑫利
     */
    @Override
    public JsonResult sendBackAllocateProcessing(@RequestBody UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result = new JsonResult();
        //对参数的合理性判断
        if (updateProductAllotApplyDTO.getTypeName().isEmpty()
                || updateProductAllotApplyDTO.getTypeId() == null
                || updateProductAllotApplyDTO.getTypeId() <= 0
                || updateProductAllotApplyDTO.getDisposer().isEmpty()
                || updateProductAllotApplyDTO.getApplyStatus() == null
                || updateProductAllotApplyDTO.getApplyStatus() <= 0
                || updateProductAllotApplyDTO.getAllotNumber() == null
                || updateProductAllotApplyDTO.getAllotNumber() <= 0
                || updateProductAllotApplyDTO.getProductId() == null
                || updateProductAllotApplyDTO.getProductId() <= 0) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数不可为空");
            return result;
        }
        ProductInventory productInventory = productInventoryDao.queryProductInventoryById(updateProductAllotApplyDTO.getProductId());
        //修改库存量
        productInventory.setSurplus(productInventory.getSurplus() + updateProductAllotApplyDTO.getAllotNumber());
        //操作数据库 true：成功 false：失败
        boolean bRet = productInventoryDao.updateProductInventory(updateProductAllotApplyDTO) > 0;
        if (!bRet) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("退货失败");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setMessage("退货成功");
        return result;
    }

    /**
     * 查询调货次数前十商品类别
     *
     * @return 商品类别信息
     * @author 季鑫利
     */
    @Override
    public JsonResult<List<ProductAllotApply>> queryAllotsTopTen() {
        JsonResult result = new JsonResult();
        List<ProductAllotApply> productAllotApplies = productAllotApplyDao.queryAllotsTopTen();
        if (productAllotApplies.size() <= 0) {
            result.setStatus(ResultCodeEM.NOCONTENT);
            result.setMessage("未有任何商品调拨操作");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setData(productAllotApplies);
        return result;
    }

    /**
     * 查询断货次数前十商品类别
     *
     * @return 商品类别信息
     * @author 季鑫利
     */
    @Override
    public JsonResult<List<ProductAllotApply>> querySupplyShortageTopTen() {
        JsonResult result = new JsonResult();
        List<ProductAllotApply> productAllotApplies = productAllotApplyDao.querySupplyShortageTopTen();
        if (productAllotApplies.size() <= 0) {
            result.setStatus(ResultCodeEM.NOCONTENT);
            result.setMessage("未有商品断货");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setData(productAllotApplies);
        return result;
    }

    /**
     * 查询柜台调货次数前十商品类别
     *
     * @return 商品类别信息
     * @author 季鑫利
     */
    @Override
    public JsonResult<List<ProductAllotApply>> queryCountersNumberTopTen() {
        JsonResult result = new JsonResult();
        List<ProductAllotApply> productAllotApplies = productAllotApplyDao.queryCountersNumberTopTen();
        if (productAllotApplies.size() <= 0) {
            result.setStatus(ResultCodeEM.NOCONTENT);
            result.setMessage("未有柜台调货记录");
            return result;
        }
        result.setStatus(ResultCodeEM.OK);
        result.setData(productAllotApplies);
        return result;
    }

    /**
     * BEGIN PRIVATE METHOD
     * 检验参数
     *
     * @param updateProductAllotApplyDTO 更新调拨参数实体
     * @return 检验结果
     * @author 季鑫利
     */
    private static JsonResult checkUpdateProductAllotApplyDTO(UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result = new JsonResult();
        //对参数的合理性判断
        if (updateProductAllotApplyDTO.getAllotNumber() == null
                || updateProductAllotApplyDTO.getAllotNumber() <= 0
                || updateProductAllotApplyDTO.getId() == null
                || updateProductAllotApplyDTO.getId() <= 0
                || updateProductAllotApplyDTO.getProductId() == null
                || updateProductAllotApplyDTO.getProductId() <= 0
                || updateProductAllotApplyDTO.getApplyStatus() == null
                || updateProductAllotApplyDTO.getApplyStatus() <= 0
                || updateProductAllotApplyDTO.getDisposer().isEmpty()
                || updateProductAllotApplyDTO.getTypeId() == null
                || updateProductAllotApplyDTO.getTypeId() <= 0
                || updateProductAllotApplyDTO.getTypeName().isEmpty()) {
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("参数异常");
            return result;
        }
        return result;
    }
    /* END PRIVATE METHOD */
}
